package com.keeson.android_cli.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.keeson.android_cli.R;
import com.keeson.android_cli.util.ResUtils;
import com.keeson.android_cli.util.StringUtils;


/**
 * 自定义对话框
 * 支持单按钮，双按钮
 */
public class CommonDialog extends Dialog {

    private String mTip;
    private String leftText;
    private String rightText;
    private String middleText;

    private OnDialogListener mLeftListener;
    private OnDialogListener mRightListener;
    private OnDialogListener mMiddleListener;

    public CommonDialog(Context context, String tip, String leftText, String rightText,
                        OnDialogListener left, OnDialogListener right) {
        super(context, R.style.dialogs);
        mTip = tip;
        this.leftText = leftText;
        this.rightText = rightText;
        this.mLeftListener = left;
        this.mRightListener = right;
    }


    public CommonDialog(Context context, String tip, String middleText, OnDialogListener middle) {
        super(context, R.style.dialogs);
        mTip = tip;
        this.middleText = middleText;
        this.mMiddleListener = middle;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.view_dialog);
        TextView tvTip = findViewById(R.id.tv_tip);
        LinearLayout llLeftView = findViewById(R.id.ll_left_right);
        TextView tvLeft = findViewById(R.id.tv_left);
        TextView tvRight = findViewById(R.id.tv_right);
        TextView tvMiddle = findViewById(R.id.tv_middle);
        if (StringUtils.isEmpty(middleText)) {
            llLeftView.setVisibility(View.VISIBLE);
            tvMiddle.setVisibility(View.GONE);
            tvLeft.setBackground(ResUtils.getDrawable(R.drawable.shape_btn_dialog_left));
            tvRight.setBackground(ResUtils.getDrawable(R.drawable.shape_btn_dialog_right));
        } else {
            tvMiddle.setVisibility(View.VISIBLE);
            llLeftView.setVisibility(View.GONE);
            tvMiddle.setBackground(ResUtils.getDrawable(R.drawable.shape_btn_dialog));
        }
        tvTip.setText(mTip);
        tvLeft.setText(leftText);
        tvRight.setText(rightText);
        tvMiddle.setText(middleText);
        tvLeft.setOnClickListener(view -> {
            dismiss();
            if (mLeftListener != null) {
                mLeftListener.onClick(view);
            }
        });
        tvRight.setOnClickListener(view -> {
            dismiss();
            if (mRightListener != null) {
                mRightListener.onClick(view);
            }
        });
        tvMiddle.setOnClickListener(view -> {
            dismiss();
            if (mMiddleListener != null) {
                mMiddleListener.onClick(view);
            }
        });
        setCanceledOnTouchOutside(false);
        setCancelable(false);

    }

    public interface OnDialogListener {
        void onClick(View view);
    }
}
